package development.listener;

import java.util.Vector;

import state.socket.DataPackage;
import state.util.Hashtable;
import development.Global;
import development.bean.SMSServiceCommand;
import development.socket.ProtocolConstants;

public class SMSServiceCommandListener extends Listener {

  public SMSServiceCommandListener() {
    super(SMSServiceCommandListener.class.toString());
  }

  public void onConnectDone() {
  }

  public void onConnectFail() {
  }

  public void onDisconnect() {
  }

  public void onRecieveData(DataPackage dataPackage) {
    int header = dataPackage.getHeader();
    switch (header) {
    case ProtocolConstants.ResponseHeader.SUBSCRIPTION_SMS_COMMAND_RESPONSE:
      onSMSServiceCommandReceive(dataPackage);
      break;
    }
  }

  private void onSMSServiceCommandReceive(DataPackage dataPackage) {
    String service = dataPackage.nextString();
    String command = dataPackage.nextString();
    String serviceNumber = dataPackage.nextString();
    Hashtable smsServiceCommandMap = (Hashtable) Global.smsServiceCommand.get(String.valueOf(Global.currentUser.getProviderId()));
    if (smsServiceCommandMap == null || smsServiceCommandMap.isEmpty()) {
      smsServiceCommandMap = new Hashtable();
    }
    Vector serviceCommandList = (Vector) smsServiceCommandMap.get(service);
    if (serviceCommandList == null) {
      serviceCommandList = new Vector();
    }
    SMSServiceCommand serviceCommand = new SMSServiceCommand();
    serviceCommand.serviceCode = service;
    serviceCommand.command = command;
    serviceCommand.serviceNumber = serviceNumber;
    serviceCommandList.addElement(serviceCommand);

    smsServiceCommandMap.put(service, serviceCommandList);
    Global.smsServiceCommand.put(String.valueOf(Global.currentUser.getProviderId()), smsServiceCommandMap);
  }
}
